<?php

class SmsController extends MyClass_Action {
	protected $controller;
	protected $action;
	protected $_user;
	protected $sms_user='zs_yunxiaoche';
	protected $sms_pwd='asdfgxfc123';
	protected $url="http://223.203.192.230:8080/qxt/";
	public function init() {
		
		$this->commonAction();	
	
	}
	//首页
	public function indexAction() {
		try {
			
			$count = 30;
			$curpage = ( int ) $this->_request->getParam ( 'page' );
	     	if (! $curpage)
    				$curpage = 1;
    			$offset = ($curpage - 1) * $count; 
			
			$result = $this->_dbAdapter->fetchAll ("select SQL_CALC_FOUND_ROWS *  from sms_sendlog where isdelete=0 order by is_send desc,sendtime desc limit $offset,$count");
			//print_r($result);
			$this->view->result=$result;
			
    			$param ['file'] = '/' . $this->controller . '/' . $this->action;
    			$param ['totalnum'] = $this->_dbAdapter->fetchOne ( 'SELECT FOUND_ROWS()' ); 
    			$param ['perpagenum'] = $count; // 每页显示的数目
    			$param ['disnum'] = 11; // 取单数显示，当前页停在中间
    			$param ['curpage'] = $curpage; // 当前页码
				$this->view->allnum=$param ['totalnum'];

    			$page = new MyClass_Page ( $param );
    		//	$page->setvar ( array ('year' => $year,'week'=>$week,'foodtimeid'=>$foodtimeid) );
    			//$this->_smarty->assign ( 'page', $page->getNumPage () ); 
				$this->view->page= $page->getNumPage (); 
			$u=$this->url.'surplus?user='.$this->sms_user.'&password='.md5($this->sms_pwd);
			$r=$this->get($u,'');
			$ye="";
			//echo $r;exit;
			$rs=substr($r, 0,2);
			if($rs=='ok'){
				if($r){
					$r1=explode(':mms',$r);
					//print_r($r1);exit;
					$dx=substr($r1[0], 7);
					$cx=substr($r1[1], 1);
					$ye='短信余额:'.$dx.';彩信余额:'.$cx;
				}
			}
			$rs = $this->_dbAdapter->fetchAll ("select id from sms_sendlog where is_send=1 and isdelete<>1 ");	//已发送条数
			if($rs){
				$this->view->send= count($rs); 
			}
			$rs1 = $this->_dbAdapter->fetchAll ("select id from sms_sendlog where is_send=0 and isdelete<>1 ");	//已发送条数
			if($rs1){
				$this->view->wfs= count($rs1); 
			}
			$this->view->ye= $ye; 
			$this->view->result= $result; 
		
		} catch ( Exception $e ) {
			$this->feedback ( $e->getMessage (), '注意', 'javascript:window.history.back();', 'warning' );
		}
	}
	public function addAction(){
		try {
			//$this->view->result= $result; 
			//echo $this->dbcache;exit;
		} catch ( Exception $e ) {
			$this->feedback ( $e->getMessage (), '注意', 'javascript:window.history.back();', 'warning' );
		}
	}

	public function opAction(){
		try {
			$p=$this->_request->getPost();
			$numberlist=array();//存储要发送的手机号1维数组
			//exit;
			//判断输入的手机号码，如果正确，则处理号码。
			if(!empty($p['tele'])){
				$p['tele'] = str_replace('，',',',$p['tele']);//逗号转换
				$p['tele'] = str_replace("\n",'',$p['tele']);//去除空格
				$p['tele'] = str_replace("\r",'',$p['tele']);//去除空格
				$p['tele'] = preg_replace('/\s*/','',$p['tele']);//去除空格
				$t=$p['tele'];
				$p['tele']=explode(',',$p['tele']);
				foreach($p['tele'] as $key => $value){
					if(!empty($value))
						$numberlist[]=trim($value);
				}
				$numberlist=array_unique($numberlist);//去除重复电话
				if($p['content']!=''){
					$content=$p['content'];
					$a=mb_detect_encoding($p['content'], array("ASCII","UTF-8","GB2312","GBK","BIG5"));
					if ($a == "UTF-8"){
						$p['content']=iconv("UTF-8", "GBK", $p['content']);
						$p['content']=URLEncode($p['content']);
					}
					$cg=0;
					$sb=0;
					$sbtel='失败号码：';
					$ts='';
					foreach($p['tele'] as $key => $value){
						$url=$this->url.'smssendernew?user='.$this->sms_user.'&password='.md5($this->sms_pwd).'&tele='.$value.'&msg='.$p['content'].'&extno=1&type=1';
						$data='';
						$r=$this->get($url,$data);
						//echo $r;exit;
						$rs=substr($r, 0,2);
						$sql="select  * from sms_sendlog where phonenumber=".$value;
						$sms_info=$this->_dbAdapter->fetchAll( $sql );
						$addtime=time();
						if($rs=='ok'){
							$data=array(
								'is_send' =>'1',
								'content' =>$content,		
								'sendtime' =>$addtime,		 
						     ); 
						     if($sms_info){//如果数据有数据则更新
						     	$this->_dbAdapter->update( 'sms_sendlog', $data, 'phonenumber='.$value ); 
						     }else{//没数据则插入
								$data=array(
									'phonenumber'=>$value,
									'content'=>$content,
									'sendtime'=>$addtime,
									'is_send' =>'1',
								);
								$res=$this->_dbAdapter->Insert('sms_sendlog',$data);
						     }
						     $cg=$cg+1;
						     
						}else{
							$errordata=array(
								'error' =>$rs,				 
						     ); 
							 if($sms_info){//如果数据有数据则更新
						     	$this->_dbAdapter->update( 'sms_sendlog', $errordata, 'phonenumber='.$value ); 
						     }else{
						     	$data=array(
									'phonenumber'=>$value,
									'content'=>$content,
									'addtime'=>$addtime,
						     		'error' =>$rs,
								);
								$res=$this->_dbAdapter->Insert('sms_sendlog',$data);
						     }
						     $sb=$sb+1;
						}
					}	
				
				     if($sb==0){
				     	$sbtel='';
				     }
					$this->feedback ( '操作提示:成功 '.$cg.'条<br/>失败 '.$sb.'条，<br/>'.$sbtel, '提示', '/sms/add', 'tip', 15);
				}else{
			 		throw new Exception ( '短信内容为空' );
				}
				
			 }else{
			 	throw new Exception ( '未填写手机号码' );
			 }
		} catch ( Exception $e ) {
			$this->feedback ( $e->getMessage (), '注意', 'javascript:window.history.back();', 'warning' );
		}
	}
	public function deleteAction(){
		try {
			$id = ( int ) $this->_request->getParam ( 'id' );
			if (! $id)
				throw new Exception ( '参数错误' );
			$sql1="select  * from sms_sendlog where id=".$id;
			$sms_info=$this->_dbAdapter->fetchAll( $sql1 );
			if(!$sms_info)
			{
				throw new Exception ( '数据不存在或者已经删除' );
			}
			$row=array(
				'isdelete' =>'1',				 
			     );  
			 $del_result  =  $this->_dbAdapter->update( 'sms_sendlog', $row, 'id='.$id );  
			if ($del_result) {
			 	$this->feedback ( '删除成功', 'success', '/sms/index', 'tip', 5 );
			 } else {
			 	throw new Exception ( '删除失败' );
			 }		
			
		} catch ( Exception $e ) {
			$this->feedback ( $e->getMessage (), 'note', 'javascript:window.history.back();', 'warning' );
		}
	}
	 
	 //以下内容保持不变
	public function noRouteAction() {
		$this->_redirect ( '/' );
	}
	
	public function __call($method, $args) {
		if ('Action' == substr ( $method, - 6 )) {
			$url = '/' . $this->controller . '/index';
			return $this->_redirect ( $url );
		}
		throw new Exception ( 'Invalid method' );
	}
}

